package Utils;


import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

//计算一元二次方程，abc范围均在-∞~+∞，支持虚数、分数小数同时显示
public class ComputeTheSecondFormula {

    private static final double PI = 3.1415926535;

    //测试
    public static void main(String[] args) {
       // computeTheSecondFormula();
        enhanceComputeTheSecondFormula();
       // System.out.printf("测试：%.2f",BigDecimal.valueOf(-1).multiply(BigDecimal.valueOf(-4)));
    }


    public static void computeTheSecondFormula() {
        try {
            Scanner scanner = new Scanner(System.in);
            //输入最高项系数
            System.out.println("输入最高项系数");
            double a = Double.parseDouble(scanner.next());
            //次项系数
            System.out.println("次项系数");
            double b = Double.parseDouble(scanner.next());
            //常数项
            System.out.println("常数项");
            double c = Double.parseDouble(scanner.next());
            //check
            double check = b * b - 4 * a * c;
            if (check > 0) {
                double sqrtValue = Math.sqrt(check);
                double x1 = (-b + sqrtValue) / (2 * a);
                double x2 = (-b - sqrtValue) / (2 * a);
                if (x1 > x2) {
                    double temp = x1;
                    x1 = x2;
                    x2 = temp;
                }
                System.out.printf("有两个解：x1 = %.2f，x2 = %.2f", x1, x2);
                System.out.println();
            } else if (check == 0) {
                System.out.printf("有一个解：x = %.2f", (-b / (2 * a)));
                System.out.println();
            } else {
                double sqrtValue = Math.sqrt(Math.abs(check));
                double leftPart = -b / (2 * a);
                double rightPart = sqrtValue / (2 * a);
                System.out.printf("x1 = %.2f + %.2fi，x2 = %.2f - %.2fi", leftPart, rightPart, leftPart, rightPart);
                System.out.println();
                System.out.printf("即：x1 = %.2f/%.2f + %.2f/%.2fi，x2 = %.2f/%.2f - %.2f/%.2fi", -b, (2 * a), sqrtValue, (2 * a), -b, (2 * a), sqrtValue, (2 * a));
                System.out.println();
            }
        } catch (Exception e) {
            System.out.println("异常问题，请重试");
        }
    }

    //加强
    public static void enhanceComputeTheSecondFormula() {
        try {
            Scanner scanner = new Scanner(System.in);
            //输入最高项系数
            System.out.println("输入最高项系数");
            double aDouble = Double.parseDouble(scanner.next());
            BigDecimal a = BigDecimal.valueOf(aDouble);
            //次项系数
            System.out.println("次项系数");
            double bDouble = Double.parseDouble(scanner.next());
            BigDecimal b = BigDecimal.valueOf(bDouble);
            //常数项
            System.out.println("常数项");
            double cDouble = Double.parseDouble(scanner.next());
            BigDecimal c = BigDecimal.valueOf(cDouble);
            //check
            BigDecimal check = b.multiply(b).subtract(BigDecimal.valueOf(4).multiply(a).multiply(c));//b * b - 4 * a * c;
            if (check.signum() > 0) {
                BigDecimal sqrtValue = sqrt(check);
                BigDecimal x1 = BigDecimal.valueOf(-1).multiply(b).add(sqrtValue).divide(BigDecimal.TWO.multiply(a), 2, RoundingMode.HALF_UP);       //(-b + sqrtValue) / (2 * a);
                BigDecimal x2 = BigDecimal.valueOf(-1).multiply(b).subtract(sqrtValue).divide(BigDecimal.TWO.multiply(a), 2, RoundingMode.HALF_UP);      //(-b - sqrtValue) / (2 * a);
                if (x1.compareTo(x2) > 0) {
                    BigDecimal temp = x1;
                    x1 = x2;
                    x2 = temp;
                }
                System.out.printf("有两个解：x1 = %.2f，x2 = %.2f", x1, x2);
                System.out.println();
            } else if (check.compareTo(BigDecimal.valueOf(0)) == 0) {
                System.out.printf("有一个解：x = %.2f", BigDecimal.valueOf(-1).multiply(b).divide(BigDecimal.TWO.multiply(a), 2, RoundingMode.HALF_UP));
                System.out.println();
            } else {
                BigDecimal sqrtValue = sqrt(BigDecimal.valueOf(-1).multiply(check));
                BigDecimal leftPart =  BigDecimal.valueOf(-1).multiply(b).divide(BigDecimal.TWO.multiply(a), 2, RoundingMode.HALF_UP);//-b / (2 * a);
                BigDecimal rightPart = sqrtValue.divide(BigDecimal.TWO.multiply(a), 2, RoundingMode.HALF_UP);//sqrtValue / (2 * a);
                System.out.printf("x1 = %.2f + %.2fi，x2 = %.2f - %.2fi", leftPart, rightPart, leftPart, rightPart);
                System.out.println();
                System.out.printf("即：x1 = %.2f/%.2f + %.2f/%.2fi，x2 = %.2f/%.2f - %.2f/%.2fi", BigDecimal.valueOf(-1).multiply(b), BigDecimal.TWO.multiply(a), sqrtValue, BigDecimal.TWO.multiply(a), BigDecimal.valueOf(-1).multiply(b), BigDecimal.TWO.multiply(a), sqrtValue, BigDecimal.TWO.multiply(a));
                System.out.println();
            }
        } catch (Exception e) {
            System.out.println("异常问题，请重试");
        }
    }

    /*
    * 输入最高项系数
4345362
次项系数
0897453
常数项
343512
x1 = -0.10 + 0.26i，x2 = -0.10 - 0.26i
即：x1 = -897453.00/8690724.00 + 2272732.73/8690724.00i，x2 = -897453.00/8690724.00 - 2272732.73/8690724.00i
    *
    *
    * */
    //开根号
    public static BigDecimal sqrt(BigDecimal value) {
        BigDecimal x = BigDecimal.valueOf(Math.sqrt(value.doubleValue()));
        return x.add(BigDecimal.valueOf(value.subtract(x.multiply(x)).doubleValue()));
    }
}
